<?php
/**
 * 来店管控时段数据
 * @author qianxun
 *
 */
class reachTimeUnitModel extends MY_Model{
    function reachTimeUnitModel(){
        parent::MY_Model();
        $this->table = 'ReachTimeUnit';
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //时段配置列表
        $this->load->model('timeUnitModel');
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        foreach ($list as $k => $v){
            //时段名称
            $v['timeUnitStr'] = isset($timeUnitDataList[$v['year']][$v['timeUnit']]) ? $timeUnitDataList[$v['year']][$v['timeUnit']]['name'] : '';
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 初始化主流程数据
     * @param array $data
     */
    function initData($where = array(), $data = array()){
        if (!isset($where['storeId']) || !$where['storeId']){
            showError('请设置店铺ID');
        }
        if (!isset($where['year']) || !$where['year']){
            showError('请设置年份');
        }
        if (!isset($where['timeUnit']) || !$where['timeUnit']){
            showError('请设置时间');
        }
        $data['createTime'] = time();
        $procId = $this->upset($where, $data);
        if ($procId){
            return $procId;
        }else{
            showError('生成失败');
        }
    }
    /**
     * 获取子类金额
     * @param number $id
     */
    function getChildMoney($id){
        $this->load->model('reachWeekModel');
        $weekData = $this->reachWeekModel->getMoney($id);
        $data = array(
            'comeStoreActual' => $weekData['comeStoreActual'],//来店实际
            'pickCarActual' => $weekData['pickCarActual'],//提车实际
            'deliveryActual' => $weekData['deliveryActual'],//交车实际
        );
        return $data;
    }
    /**
     * 更新金额
     * @param number $id
     */
    function updateMoney($id){
        $findData = $this->getData($id);
        $childMoney = $this->getChildMoney($id);
        $data = array(
            'comeStoreTarget' => (int)$findData['comeStoreTarget'],
            'comeStoreActual' => $childMoney['comeStoreActual'],
            'pickCarActual' => $childMoney['pickCarActual'],
            'deliveryActual' => $childMoney['deliveryActual'],
        );
        $data['comeStoreRate'] = $data['comeStoreTarget'] ? ($data['comeStoreActual'] / $data['comeStoreTarget'])*100 : 0;
        $data['updateTime'] = time();
        $this->edit($id, $data);
    }
    /**
     * 获取专营店双月列表
     * @param number $storeId
     * @param number $year
     * @return array
     */
    function getStoreListByTimeUnit($storeId, $year){
        $findList = $this->getNewList(array('storeId'=>$storeId,'year'=>$year));
        $list = array();
        foreach ($findList as $k => $v){
            $list[$v['timeUnit']] = $v;
        }
        return $list;
    }
}